//
//  NewsCellViewModel.m
//  objc_property_t 的使用
//
//  Created by dllo on 16/7/15.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "NewsViewModel.h"
#import "NewsModel.h"
#import "NSObject+Runtime.h"

@implementation NewsViewModel

- (void)getData:(blockWithArray)blockArray {
    
    NSString *path = [[NSBundle mainBundle]pathForResource:@"news" ofType:@"txt"];
    
    NSData *data = [NSData dataWithContentsOfFile:path];
    id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
    
    NSArray *newsArr = [result objectForKey:@"news"];
    // 打开为KVC赋值
    
    //NSArray *arrOfData = [self setValueWithKVC:newsArr];
    NSArray *arrOfData = [self setValueWithObjcProperty:newsArr];
    
    blockArray(arrOfData);
    
}

- (NSArray *)setValueWithKVC:(NSArray *)arr
{

    NSMutableArray *mArrOfData = [NSMutableArray array];
    for (NSDictionary *dic in arr) {
        NewsModel *model = [[NewsModel alloc]init];
        [model setValuesForKeysWithDictionary:dic];
        [mArrOfData addObject:model];
        
    }
    return mArrOfData;
}

- (NSArray *)setValueWithObjcProperty:(NSArray *)arr
{
    NSMutableArray *mArrOfData = [NSMutableArray array];
    for (NSDictionary *dic in arr) {
        
        NewsModel *model = [[NewsModel alloc]init];
        [model reflectDataFromOtherObject:dic];
        [mArrOfData addObject:model];
    }
    
    return mArrOfData;
}

@end
